package com.nny.alarm.widget;

import com.nny.alarm.widget.HandView.HandViewAdapter;
import com.nny.alarm.R;
import com.uxwine.widget.CycleButton;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;

public class DialView extends FrameLayout implements 
	HandViewAdapter, 
	IDialAdapter.AdapterView,
	HandView.OnMarkListener,
	View.OnClickListener
{
	HandView mHandView;
	CycleButton mBtnNav;
	ImageView mMarkerView;
	ImageView mNavName;
	IDialAdapter mAdapter;
	OnDialListener mOnDialListener;
	
	public DialView(Context context, AttributeSet attrs)
	{
		super(context, attrs);
		initView();
	}
	
	private void initView()
	{
		View.inflate(getContext(), R.layout.view_dial, this);
	}

	@Override
	protected void onFinishInflate()
	{
		mHandView = (HandView)findViewById(R.id.dial_hand);
		mBtnNav = (CycleButton)findViewById(R.id.dial_nav);
		mMarkerView = (ImageView)findViewById(R.id.dial_marker);
		mNavName = (ImageView)findViewById(R.id.dial_nav_name);
		
		mHandView.setAdapter(this);
		mHandView.setOnMarkListener(this);
		
		mBtnNav.setOnClickListener(this);
	}

	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh)
	{
		super.onSizeChanged(w, h, oldw, oldh);
		mHandView.setRadious(mMarkerView.getMeasuredWidth() / 2);
	}

	@Override
	public int getCount()
	{
		if(mAdapter == null)
			return 1;
		return mAdapter.getMarkCount();
	}

	@Override
	public String getMarkText(int index)
	{
		if(mAdapter == null)
			return null;
		return mAdapter.getMarkText(index);
	}

	@Override
	public int getTextColor(int index)
	{
		if(mAdapter == null)
			return Color.BLACK;
		return mAdapter.getMarkColor(index);
	}

	@Override
	public int getMax()
	{
		if(null != mAdapter)
			return mAdapter.getMax();
		return 0;
	}
	
	public void setAdapter(IDialAdapter adpater)
	{
		mAdapter = adpater;
		if(null != mAdapter)
			mAdapter.setView(this);
		mAdapter.notifyDataSetChanged();
	}

	@Override
	public void notifyDataSetChanged()
	{
		if(null == mAdapter)
			return;
		mNavName.setImageResource(mAdapter.getBtnDrawable());
		mMarkerView.setImageResource(mAdapter.getDialDrawable());
		mHandView.setPosition(mAdapter.getPosition());
		mHandView.invalidate();
	}
	
	public interface OnDialListener
	{
		public void onNavClicked();
		public boolean onMarked(int position);
		public boolean onMarkChanged(int position);
	}

	@Override
	public void onClick(View v)
	{
		if(null != mOnDialListener)
			mOnDialListener.onNavClicked();
	}

	@Override
	public boolean onMarkChanged(int position)
	{
		boolean handled = true;
		if(null != mOnDialListener)
			handled = mOnDialListener.onMarkChanged(position);
		if(null != mAdapter && handled)
			mAdapter.setPosition(position);
		return false;
	}

	@Override
	public void onMarked(int position)
	{
		boolean handled = true;
		if(null != mOnDialListener)
			handled = mOnDialListener.onMarked(position);
		if(null != mAdapter && handled)
			mAdapter.setPosition(position);	
	}
	
	public void setOnDialListener(OnDialListener l)
	{
		mOnDialListener = l;
	}
}
